<?php
namespace app\Admin\controller;
use think\Controller;
use think\captcha\Captcha;
use think\Db;

class Login extends Controller
{
	//登录页面
    function login()
    {
        // $this->redirect("admin/Admin/admin");
        return $this->fetch();

    }

	//登录验证
    function admin($user, $pwd, $code)
    {
        $captcha = new Captcha();
        if( !$captcha->check($code))
        {
            return $this->error("验证码错误");
        }

        $retval = model('Admin')->verificate($user, $pwd);
        if(!$retval){
            return $this->error('账号或密码错误');
        }else{
        	session('user',$retval);
        	$usr = session('user');
        	db('administrator')->where('id',$usr['id'])->update(['time'=>time()]);
        	$this->redirect("admin/admin/admin");
            // return $this->fetch();
        }
    }

    //退出
    function exituser(){
    	session('user',null);
    	// $this->redirect("admin/admin/admin");
        $this->redirect("admin/login/login");
    }

    //修改密码页面
    function pwd()
    {
        return $this->fetch();
    }


    function modifypwd($nickname, $oldpwd, $newpwd1, $newpwd2)
    {
        if($oldpwd!=session('user')['password']){
            return $this->error('旧密码错误');
        }
        if($newpwd1!=$newpwd2){
            return $this->error('两次输入密码不一致');
        }
        $data = ['password'=>$newpwd1, 'nickname'=>$nickname];
        db('administrator')->where('id',session('user')['id'])->update($data);
        return $this->success('修改成功','login');
    }

    //生成验证码
    public function code()
    {
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    30,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    true,
            //设置验证码字符为纯数字
            "codeSet"      =>    "0123456789",
            // 验证码过期时间（s）
            // "expire"       =>   60,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }

}